package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.Region;
import cn.wolfcode.luowowo.article.domain.StrategyCatalog;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.query.StrategyCatalogQuery;
import cn.wolfcode.luowowo.article.query.StrategyQuery;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.*;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("destination")
public class DestinationController {

    @Reference
    private IRegionService regionService;

    @Reference
    private IDestinationService destinationService;

    @Reference
    private IStrategyCatalogService strategyCatalogService;

    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private ITravelService travelService;


    @RequestMapping("")
    public String index(Model model){

        //获取热门区域
        List<Region> list = regionService.queryHotRegions();

        model.addAttribute("hotRegions",list);

        return "destination/index";
    }


    @RequestMapping("/getHotDestByRegionId")
    public Object getHotDestByRegionId(Model model,Long regionId){


        List<Destination> destinations = destinationService.queryHotDestByRegionId(regionId);

        //左边数据
        List<Destination> left = destinations.subList(0,destinations.size() / 2 + 1);

        //右边数据
        List<Destination> right = destinations.subList(destinations.size() / 2 + 1,destinations.size());

        //把数据共享回去
        model.addAttribute("leftDests",left);

        model.addAttribute("rightDests",right);

        model.addAttribute("regionId",regionId);

        return "destination/hotdestTpl";
    }

    @RequestMapping("/guide")
    public Object guide(Model model,Long id){

        //中国-广东-广州
        List<Destination> toasts = destinationService.getToasts(id);
        //相当于把广州移除出来共享给面包屑导航,否则会出现重复
        Destination dest = toasts.remove(toasts.size() - 1);

        model.addAttribute("toasts",toasts);
        model.addAttribute("dest",dest);

        List<StrategyCatalog> catalogs =strategyCatalogService.queryByDestId(id);
        model.addAttribute("catalogs",catalogs);

        //查前三排的广州攻略 strategyDetails
        List<StrategyDetail> strategyDetails = strategyDetailService.queryViewNumTop3(id);
        model.addAttribute("strategyDetails",strategyDetails);


        return "destination/guide";
    }
    //查看情况分类明细
    @RequestMapping("/surveyPage")
    public Object surveyPage(Model model,@ModelAttribute("qo") StrategyCatalogQuery qo){

        //中国-广东-广州
        List<Destination> toasts = destinationService.getToasts(qo.getDestId());
        //相当于把广州移除出来共享给面包屑导航,否则会出现重复
        Destination dest = toasts.remove(toasts.size() - 1);
        model.addAttribute("toasts",toasts);
        model.addAttribute("dest",dest);


        return "destination/survey";
    }
    //查看情况分类明细
    @RequestMapping("/survey")
    public Object survey(Model model, @ModelAttribute("qo") StrategyCatalogQuery qo){

        //catalogs
        List<StrategyCatalog> catalogs = strategyCatalogService.queryByDestId(qo.getDestId());
        model.addAttribute("catalogs",catalogs);

        StrategyCatalog catalog = null;
        for (StrategyCatalog c : catalogs){
            if(c.getId() == qo.getCatalogId()){
                catalog = c;
                model.addAttribute("catalog",catalog);
            }
        }

        //detail
        if(catalog != null){
            List<StrategyDetail> details = catalog.getDetails();
            if(details !=null && details.size() > 0){
                //获取内容
                StrategyDetail detail = details.get(0);
                detail.setStrategyContent(strategyDetailService.getContent(detail.getId()));
                model.addAttribute("detail",details.get(0));
            }
        }

        return "destination/surveyTpl";
    }
    //查看游记
    @RequestMapping("/travels")
    public Object travels(Model model,@ModelAttribute("qo")TravelQuery qo){

        model.addAttribute("pageInfo",travelService.query(qo));

        return "destination/travelTpl";
    }
}
